<!DOCTYPE html>
<html>
<head>
  <title>Tests AccessFu content integration</title>
  <meta charset="utf-8" />
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />

  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js">
  </script>
  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js">
  </script>
  <script type="application/javascript"
          src="chrome://mochikit/content/chrome-harness.js">
  </script>

  <script type="application/javascript" src="../common.js"></script>
  <script type="application/javascript" src="../browser.js"></script>
  <script type="application/javascript" src="../events.js"></script>
  <script type="application/javascript" src="../role.js"></script>
  <script type="application/javascript" src="../states.js"></script>
  <script type="application/javascript" src="../layout.js"></script>
  <script type="application/javascript" src="jsatcommon.js"></script>

  <script type="application/javascript">
    function doTest() {
      var doc = currentTabDocument();
      var textTest = new AccessFuContentTest(
        [
          // Read-only text tests
          [ContentMessages.simpleMoveFirst,
           new ExpectedCursorChange(
            ['Text content test document', 'These are my awards, Mother. ' +
             'From Army. The seal is for marksmanship, and the gorilla is ' +
             'for sand racing.'])],
          [ContentMessages.moveNextBy('word'),
           new ExpectedCursorTextChange('These', 0, 5)],
          [ContentMessages.moveNextBy('word'),
           new ExpectedCursorTextChange('are', 6, 9)],
          [ContentMessages.moveNextBy('word'),
           new ExpectedCursorTextChange('my', 10, 12)],
          [ContentMessages.moveNextBy('word'),
           new ExpectedCursorTextChange('awards,', 13, 20)],
          [ContentMessages.moveNextBy('word'),
           new ExpectedCursorTextChange('Mother.', 21, 28)],
          [ContentMessages.movePreviousBy('word'),
           new ExpectedCursorTextChange('awards,', 13, 20)],
          [ContentMessages.movePreviousBy('word'),
           new ExpectedCursorTextChange('my', 10, 12)],
          [ContentMessages.movePreviousBy('word'),
           new ExpectedCursorTextChange('are', 6, 9)],
          [ContentMessages.movePreviousBy('word'),
           new ExpectedCursorTextChange('These', 0, 5)],
          [ContentMessages.simpleMoveNext,
           new ExpectedCursorChange(['You\'re a good guy, mon frere. ' +
              'That means brother in French. ' +
              'I don\'t know how I know that. ' +
              'I took four years of Spanish.'])],
          // XXX: Word boundary should be past the apostraphe.
          [ContentMessages.moveNextBy('word'),
           new ExpectedCursorTextChange('You\'re', 0, 6,
             { android_todo: true /* Bug 980512 */})],

          // Editable text tests.
          [ContentMessages.focusSelector('textarea'),
           new ExpectedAnnouncement('editing'),
           new ExpectedEditState({
            editing: true,
            multiline: true,
            atStart: true,
            atEnd: false
           }),
           new ExpectedCursorChange(
            ['Please refrain from Mayoneggs during this salmonella scare.',
             {string: 'textarea'}]),
           new ExpectedTextSelectionChanged(0, 0)
          ],
          [ContentMessages.activateCurrent(10),
           new ExpectedTextCaretChanged(0, 10),
           new ExpectedEditState({ editing: true,
             multiline: true,
             atStart: false,
             atEnd: false }),
           new ExpectedTextSelectionChanged(10, 10)],
          [ContentMessages.activateCurrent(20),
           new ExpectedTextCaretChanged(10, 20),
           new ExpectedTextSelectionChanged(20, 20)
          ],
          [ContentMessages.moveCaretNextBy('word'),
           new ExpectedTextCaretChanged(20, 29),
           new ExpectedTextSelectionChanged(29, 29)
          ],
          [ContentMessages.moveCaretNextBy('word'),
           new ExpectedTextCaretChanged(29, 36),
           new ExpectedTextSelectionChanged(36, 36)
          ],
          [ContentMessages.moveCaretNextBy('character'),
           new ExpectedTextCaretChanged(36, 37),
           new ExpectedTextSelectionChanged(37, 37)
          ],
          [ContentMessages.moveCaretNextBy('character'),
           new ExpectedTextCaretChanged(37, 38),
           new ExpectedTextSelectionChanged(38, 38)
          ],
          [ContentMessages.moveCaretNextBy('paragraph'),
           new ExpectedTextCaretChanged(38, 59),
           new ExpectedTextSelectionChanged(59, 59)
          ],
          [ContentMessages.moveCaretPreviousBy('word'),
           new ExpectedTextCaretChanged(53, 59),
           new ExpectedTextSelectionChanged(53, 53)
          ],

          // bug xxx
          [ContentMessages.simpleMoveNext,
           new ExpectedCursorChange(
            ['So we don\'t get dessert?', {string: 'label'}],
            { focused: 'html'}),
           new ExpectedAnnouncement('navigating'),
           new ExpectedEditState({
            editing: false,
            multiline: false,
            atStart: true,
            atEnd: false })],
          [ContentMessages.simpleMoveNext,
           new ExpectedCursorChange(
            [{ string : 'entry' }],
            { focused: 'html'})],
          [ContentMessages.activateCurrent(0),
           new ExpectedClickAction(),
           new ExpectedAnnouncement('editing'),
           new ExpectedEditState({
            editing: true,
            multiline: false,
            atStart: true,
            atEnd: true
           }, { focused: 'input[type=text]' }),
           new ExpectedTextSelectionChanged(0, 0),
           new ExpectedTextSelectionChanged(0, 0)
           ],
          [ContentMessages.simpleMovePrevious,
           new ExpectedCursorChange(
            ['So we don\'t get dessert?', {string: 'label'}]),
           new ExpectedAnnouncement('navigating'),
           new ExpectedEditState({
            editing: false,
            multiline: false,
            atStart: true,
            atEnd: false
           },{ focused: 'html' })
         ],
          [ContentMessages.simpleMoveNext,
           new ExpectedCursorChange(
            [{ string : 'entry' }],
            { focused: 'html'})],
          [ContentMessages.activateCurrent(0),
           new ExpectedClickAction(),
           new ExpectedAnnouncement('editing'),
           new ExpectedEditState({
            editing: true,
            multiline: false,
            atStart: true,
            atEnd: true
           },
           { focused: 'input[type=text]' }),
           new ExpectedTextSelectionChanged(0, 0)],
          [ContentMessages.simpleMovePrevious,
           new ExpectedCursorChange(
            [ 'So we don\'t get dessert?', {string: 'label'} ]),
           new ExpectedAnnouncement('navigating'),
           new ExpectedEditState({
            editing: false,
            multiline: false,
            atStart: true,
            atEnd: false
           }, { focused: 'html' })],

          [ContentMessages.focusSelector('input'),
           new ExpectedAnnouncement('editing'),
           new ExpectedEditState({
            editing: true,
            multiline: false,
            atStart: true,
            atEnd: true
           }),
           new ExpectedCursorChange([{string: 'entry'}]),
           new ExpectedTextSelectionChanged(0, 0)
          ],
          [function() {
             SpecialPowers.pushPrefEnv({"set": [[KEYBOARD_ECHO_SETTING, 3]]}, typeKey('a')());
           },
           new ExpectedTextChanged('a'),
           new ExpectedTextSelectionChanged(1, 1),
          ],
          [typeKey('b'),
           new ExpectedTextChanged('b'),
           new ExpectedTextSelectionChanged(2, 2),
          ],
          [typeKey('c'),
           new ExpectedTextChanged('c'),
           new ExpectedTextSelectionChanged(3, 3),
          ],
          [typeKey('d'),
           new ExpectedTextChanged('d'),
           new ExpectedTextSelectionChanged(4, 4),
          ],
          [typeKey(' '),
           new ExpectedTextChanged(' abcd'),
           new ExpectedTextSelectionChanged(5, 5),
          ],
          [typeKey('e'),
           new ExpectedTextChanged('e'),
           new ExpectedTextSelectionChanged(6, 6),
          ],
          [function() {
             SpecialPowers.pushPrefEnv({"set": [[KEYBOARD_ECHO_SETTING, 2]]}, typeKey('a')());
           },
           new ExpectedTextChanged(''),
           new ExpectedTextSelectionChanged(7, 7),
          ],
          [typeKey('d'),
           new ExpectedTextChanged(''),
           new ExpectedTextSelectionChanged(8, 8),
          ],
          [typeKey(' '),
           new ExpectedTextChanged(' ead'),
           new ExpectedTextSelectionChanged(9, 9),
          ],
          [function() {
             SpecialPowers.pushPrefEnv({"set": [[KEYBOARD_ECHO_SETTING, 1]]}, typeKey('f')());
           },
           new ExpectedTextChanged('f'),
           new ExpectedTextSelectionChanged(10, 10),
          ],
          [typeKey('g'),
           new ExpectedTextChanged('g'),
           new ExpectedTextSelectionChanged(11, 11),
          ],
          [typeKey(' '),
           new ExpectedTextChanged(' '),
           new ExpectedTextSelectionChanged(12, 12),
          ],
          [function() {
             SpecialPowers.pushPrefEnv({"set": [[KEYBOARD_ECHO_SETTING, 0]]}, typeKey('f')());
           },
           new ExpectedTextChanged(''),
           new ExpectedTextSelectionChanged(13, 13),
          ],
          [typeKey('g'),
           new ExpectedTextChanged(''),
           new ExpectedTextSelectionChanged(14, 14),
          ],
          [typeKey(' '),
           new ExpectedTextChanged(''),
           new ExpectedTextSelectionChanged(15, 15),
          ],
        ]);

      const KEYBOARD_ECHO_SETTING = 'accessibility.accessfu.keyboard_echo';
      function typeKey(key) {
        return function() { synthesizeKey(key, {}, currentTabWindow()); };
      }

      addA11yLoadEvent(function() {
        textTest.start(function () {
          closeBrowserWindow();
          SimpleTest.finish();
        });
      }, doc.defaultView);
    }

    SimpleTest.waitForExplicitFinish();
    addLoadEvent(
      function () {
        openBrowserWindow(
          doTest,
          getRootDirectory(window.location.href) + "doc_content_text.html");
        });
  </script>
</head>
<body id="body">

  <a target="_blank"
     title="Add tests for text editing and navigating"
     href="https://bugzilla.mozilla.org/show_bug.cgi?id=972047">Mozilla Bug 933808</a>
  <p id="display"></p>
  <div id="content" style="display: none"></div>
  <pre id="test">
  </pre>
</body>
</html>
